package NEHE.JOGL.Port;

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

/**
 * NeHe Lesson 7: Texture Filters, Lighting & Keyboard Inputs (Run in Full-screen mode)
 */
public class NeheJOGL07FullScreenMain extends JFrame implements KeyListener {
   private static final String WINDOW_TITLE = "3D Shapes in Full Screen Mode";
   private static int windowWidth  = 640;  // size in non-full-screen mode
   private static int windowHeight = 480;
   
   private NeheJOGL07TextureFilterLightKey joglMain;
   private GraphicsDevice device;
   private boolean fullScreen = true; // full-screen or windowed mode

   // Constructor
   public NeheJOGL07FullScreenMain() {
      joglMain = new NeheJOGL07TextureFilterLightKey();
      this.getContentPane().add(joglMain);

      // Get the default graphic device and try full screen mode
      device = GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice();
      if (device.isFullScreenSupported()) { // go for full-screen mode
         this.setUndecorated(true);         // don't show title and border
         this.setResizable(false);
         //this.setIgnoreRepaint(true);     // ignore OS re-paint request
         device.setFullScreenWindow(this);
         fullScreen = true;
      } else { // windowed mode
         this.setSize(windowWidth, windowWidth);
         this.setResizable(true);
         fullScreen = false;
      }
      
      this.addWindowListener(new WindowAdapter() {
         @Override 
         public void windowClosing(WindowEvent e) {
            // Use a dedicate thread to run the stop() to ensure that the
            // animator stops before program exits.
            new Thread() {
               @Override 
               public void run() {
                  joglMain.animator.stop(); // stop the animator loop
                  System.exit(0);
               }
            }.start();
         }
      });

      // Enable keyboard input
      this.addKeyListener(this);
      this.setFocusable(true);  // To receive key event
      this.requestFocus();
      
      this.setTitle(WINDOW_TITLE);
      this.setVisible(true);
      joglMain.animator.start(); // start the animation loop
   }

   public static void main(String[] args) {
      new NeheJOGL07FullScreenMain();
   }
   

   // ------ Implement methods declared in KeyListener ------
   @Override
   public void keyPressed(KeyEvent e) {
      int keyCode = e.getKeyCode();
      switch (keyCode) {
         // F1 to toggle between full-screen and windowed modes
         case KeyEvent.VK_F1: 
            if (!fullScreen) {  // Saved the current size for restoration  
               Dimension screenSize = this.getSize();
               windowWidth  = (int)screenSize.getWidth();
               windowHeight = (int)screenSize.getHeight();
            }
            fullScreen = !fullScreen;
            this.setVisible(false); // Hide the display
            if (this.isDisplayable())
               this.dispose();      // For changing the decoration
            if (fullScreen) {
               if (device.isFullScreenSupported()) {
                  this.setUndecorated(true);
                  this.setResizable(false);
                  device.setFullScreenWindow(this);
               }
            } else {
               this.setUndecorated(false);  // put the title and border back
               device.setFullScreenWindow(null); // windowed mode
               this.setSize(windowWidth, windowHeight);
               this.setResizable(true);
            }
            this.setVisible(true);  // show it
            break;

         // ESC to quit
         case KeyEvent.VK_ESCAPE: 
            // Use a dedicate thread to run the stop() to ensure that the
            // animator stops before program exits.
            new Thread() {
               @Override
               public void run() {
                  joglMain.animator.stop(); // stop the animator loop
                  System.exit(0);
               }
            }.start();
            break;
         default: joglMain.keyPressed(e);
      }
   }

   @Override
   public void keyReleased(KeyEvent e) {}

   @Override
   public void keyTyped(KeyEvent e) {}
}